var addPurchaseOrderDetail = new Vue({
    el: '#addPurchaseOrderDetail',

    //初始化数据类型
    data: {
        //商品类型，没有排序
        goodsType: [],
        //商品类型，排序
        category: [],
        selectedOptions: [],
        //页面数据
        purchaseOrderDetail: {
            //品目名称
            pmmc: '',
            //品目编码
            pmbm: '',
            //规格型号
            ggxh: '',
            //主要配置及技术参数
            jscs: '',
            //单位
            dw: '',
            //数量
            sl: '',
            //单价
            dj: '',
            //总价
            zj: '',
            goodsName:'',
            goodsType:''
        },
        purchaseType: '',
        ordersDetails: [],
        purchaseOrders:'',
    },
    //时间提交方法
    methods: {
        //添加商品信息
        addPurchaseDetail: function () {
            var _this = this;
            var f_id;
            var flag;
            var ttype;
            var purchaseDetailedList;
            debugger
            //市场价格/供货价格正则
            var RegPrice = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
            ////库存正则
            var Regstock = /^[0-9]{1,5}$/;
            var name1 = $("#user-name1").val();
            var name2 = $("#user-name2").val();
            var name3 = $("#user-name3").val();
            var name4 = $("#user-name4").val();
            var name5 = $("#user-name5").val();
            var name6 = $("#user-name6").val();
            var name7 = $("#user-name7").val();
            if ((name1 == "") || (name2 == "") || (name3 == "") || (name4 == "") || (name5 == "") || (name6 == "") || (name7 == "")) {
                _this.$notify({
                    message: "关键参数为空.",
                    type: 'error'
                });
                return;
            } else {
                $.ajaxSetup({
                    async: false
                });
                if (RegPrice.test(name6) && Regstock.test(name5)) {
                    //判断这个清单是否已经存在
                    $.post("/home/purchase_orders/selectDetailById", {"data": JSON.stringify(_this.purchaseOrderDetail)}, function (data) {
                        _this.ttype = data.data.status;
                    });
                    if (_this.ttype == "true") {
                        if (_this.ordersDetails.length == 0) {
                            //如果没有朱采购单实体就认为是第一次
                            //如果没有朱采购单实体就认为是第一次
                            $.post("/home/purchase_orders/insertDetailAndAddOrders4Frist", {"data": JSON.stringify(_this.purchaseOrderDetail)}, function (data) {
                                if(data.code==200) {
                                    _this.flag = data.data.status;
                                    _this.f_id = data.data.f_id;
                                    _this.purchaseOrders = data.data.purchaseOrders;
                                }else{
                                    _this.$notify({
                                        message: data.data.message,
                                        type: 'error'
                                    });
                                    return;
                                }
                            });
                            this.selectOrders(_this.f_id);
                        } else {
                            $.post("/home/purchase_orders/addDetailByParentId", {
                                "data": JSON.stringify(_this.purchaseOrderDetail),
                                "parentData": JSON.stringify(_this.ordersDetails)
                            }, function (data) {
                                if(data.code==200) {
                                    _this.f_id = data.data.parentId;
                                    _this.flag = data.data.status;
                                }else{
                                    _this.$notify({
                                        message: data.data.message,
                                        type: 'error'
                                    });
                                    return;
                                }
                            });
                            this.selectOrders(_this.f_id);
                        }


                    } else {
                        $.ajaxSetup({
                            async: false
                        });
                        $.post("/home/purchase_orders/updateDetailById", {"data": JSON.stringify(_this.purchaseOrderDetail)},
                            function (data) {
                                _this.flag = data.data.status;
                                _this.f_id = data.data.f_id;

                            });
                        if (_this.flag === "true") {
                            _this.selectOrders(_this.f_id);
                            _this.$notify({
                                message: "清单更新成功.",
                                type: 'success'
                            });

                        }

                    }

                } else {
                    _this.$notify({
                        message: "价格或者数量格式有误.",
                        type: 'error'
                    });
                    return;
                }
            }
        },
        checkRole:function (str) {
            var _this = this;
            var RegPrice = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
            if(!RegPrice.test(str)){
                _this.$notify({
                    message: "请正确填写,必须为数字.",
                    type: 'error'
                });
            }
        },
        selectOrders: function (f_id) {
            var _this = this;
            if (_this.flag == "true") {
                $.post("/home/purchase_orders/selectPurchaseDetailedListAllByOrdersId",
                    {"ordersId": f_id},
                    function (data) {
                        //跳转添加采购单清单
                        $("#wrap-left").load("http://" + window.location.host + "/home/PurchaseAndOrdesdetail.html", function () {
                            debugger
                            PurchaseAndOrdesdetail.ordersDetails = data.data.purchaseDetailedList;
                            PurchaseAndOrdesdetail.urchaseAndOrdersdetail=_this.purchaseOrders;
                            PurchaseAndOrdesdetail.urchaseAndOrdersdetail.id = f_id;
                            PurchaseAndOrdesdetail.ordersDetails.goodsType=_this.purchaseOrderDetail.goodsType;

                        });

                    });

            }
        },
        //计算总价
        onChange: function () {
            var _this = this;
            _this.purchaseOrderDetail.zj = Number(_this.purchaseOrderDetail.dj) * Number(_this.purchaseOrderDetail.sl);
        },
        certenl: function () {
            $("#wrap-left").load("http://" + window.location.host + "/home/shopEnterExamine.html", function () {

            });

        },
        onSelectMenu: function () {
            debugger
            var _this = this;
            for (var i = 0; i < _this.goodsType.length; i++) {
                if (_this.selectedOptions[0] == _this.goodsType[i].id) {
                _this.purchaseOrderDetail.goodsType= _this.selectedOptions[0];
                    for (var j = 0; j < _this.goodsType[i].childMenus.length; j++) {
                        if (_this.selectedOptions[1] == _this.goodsType[i].childMenus[j].id) {

                            for (var z = 0; z < _this.goodsType[i].childMenus[j].childMenus.length; z++) {
                                if (_this.selectedOptions[2] == _this.goodsType[i].childMenus[j].childMenus[z].id) {

                                    _this.purchaseOrderDetail.pmbm = _this.goodsType[i].childMenus[j].childMenus[z].coding;
                                    _this.purchaseOrderDetail.pmmc = _this.goodsType[i].childMenus[j].childMenus[z].name;
                                }
                            }
                        }
                    }

                }
            }
        },
    },
    //页面加载完成后执行此方法
    created: function () {
        var _this = this;
        $.post("/home/menu/select", "", function (data) {
            _this.goodsType = data.data.Menu;
            //循环第一级菜单
            for (var i = 0; i < data.data.Menu.length; i++) {
                var map1 = {};
                map1.children = [];
                map1.value = data.data.Menu[i].id;
                map1.label = data.data.Menu[i].name;
                //循环第二级菜单
                for (var j = 0; j < data.data.Menu[i].childMenus.length; j++) {
                    var map2 = {};
                    map2.value = data.data.Menu[i].childMenus[j].id;
                    map2.label = data.data.Menu[i].childMenus[j].name;
                    map2.children = [];
                    //循环第三菜单
                    for (var z = 0; z < data.data.Menu[i].childMenus[j].childMenus.length; z++) {
                        var map3 = {};
                        map3.value = data.data.Menu[i].childMenus[j].childMenus[z].id;
                        map3.label = data.data.Menu[i].childMenus[j].childMenus[z].name;
                        map3.type = data.data.Menu[i].childMenus[j].childMenus[z].coding;
                        map2.children.push(map3);
                    }
                    map1.children.push(map2);

                }
                // debugger
                _this.category.push(map1);
            }

        });


    }
});